<?php
$timeout = 5; //Change this if you want. Default is 5 seconds.
//###################### CALCULATE USERS ONLINE #########################
$userip = $_SERVER['REMOTE_ADDR'];
$time = time();
$usersonline = 0;
$alreadyonline = 0;

$onlinetxt = fopen("./online.txt","r+");
flock($onlinetxt,2);
while (!feof($onlinetxt)) 
$user[] = chop(fgets($onlinetxt,65536));

fseek($onlinetxt,0,SEEK_SET);
ftruncate($onlinetxt,0);
foreach ($user as $line) {
  @list($savedip,$savedtime) = split("\|",$line);
  if ($savedip == $userip) {
    $savedtime = $time;
    $alreadyonline = 1;
  }
  if ($time < $savedtime + ($timeout)) {
    fputs($onlinetxt,"$savedip|$savedtime\n");
    $usersonline = $usersonline + 1;
  }
}
if (!$alreadyonline) {
  fputs($onlinetxt,"$userip|$time\n");
  $usersonline = $usersonline + 1;
}
fclose ($onlinetxt);

echo "<div id=\"online\"><strong>Gente Online</strong>: ".$usersonline."</div>";
?>
